package com.dzbh.cdsdriver.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dzbh.cdsdriver.R;

public class InputDriverActivity extends Activity {

    private Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inputdriver);

        // 取得自定义View
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        View view = layoutInflater.inflate(R.layout.dialog_inputdriver, null);
        final EditText nameET = (EditText) view.findViewById(R.id.name);
        final EditText phoneET = (EditText) view.findViewById(R.id.phoneNumber);
        final Button finishBtn = (Button) view.findViewById(R.id.ok);
        final Button cancelBtn = (Button) view.findViewById(R.id.cancel);
        finishBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = nameET.getText().toString();
                String phone = phoneET.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(InputDriverActivity.this, R.string.tip_name, Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(phone)) {
                    Toast.makeText(InputDriverActivity.this, R.string.tip_phone, Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("name", name);
                intent.putExtra("phone", phone);
                //intent.putExtra("name", "杨志");
                setResult(RESULT_OK, intent);
                if(null != dialog){
                    dialog.dismiss();
                }
            }
        });

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != dialog){
                    dialog.dismiss();
                }
            }
        });

        dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(view);
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                Log.d("dong", "onDismiss");
                finish();
            }
        });
        //dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }
}
